using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MVCWeb.Models;

namespace MVCWeb.Controllers
{
    public class AdsController : BaseController
    {
        //
        // GET: /Ads/
        [Authorize(Roles="Administrators")]
        public ActionResult Index()
        {
            return View(this.dc.Ads.OrderBy(a => a.AdsId).ToList<Ads>());
        }

        //
        // GET: /Ads/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Ads/Create
        [Authorize(Roles = "Administrators")]
        public ActionResult Create()
        {
            Ads ads = new Ads();
            return View(ads);
        } 

        //
        // POST: /Ads/Create
        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles="Administrators")]
        public ActionResult Create(Ads adsToCreate, FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                this.dc.Ads.InsertOnSubmit(adsToCreate);
                this.dc.SubmitChanges();
                if (collection["IsRedirectToView"].Equals("true"))
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return Redirect(Request.RawUrl);
                }
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Ads/Edit/5
        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(int id)
        {
            Ads ads = this.dc.Ads.Single(a => a.AdsId == id);
            return View(ads);
        }

        //
        // POST: /Ads/Edit/5
        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrators")]
        public ActionResult Edit(int id, Ads adsToEdit, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                adsToEdit = this.dc.Ads.Single(a => a.AdsId == id);
                UpdateModel<Ads>(adsToEdit);
                this.dc.SubmitChanges();
                if (collection["IsRedirectToView"].Equals("true"))
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return Redirect(Request.RawUrl);
                }
            }
            catch
            {
                return View();
            }
        }

        //
        // POST: /Ads/Edit/5
        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrators")]
        public ActionResult Delete(int id)
        {
            try
            {
                // TODO: Add update logic here
                Ads ads = this.dc.Ads.Single(a => a.AdsId == id);
                this.dc.Ads.DeleteOnSubmit(ads);
                this.dc.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
